package com.module.product.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;

import com.module.product.vo.Category;
import com.module.product.vo.CategoryProduct;
public class CategoryProductDao extends GenericHibernateDao<CategoryProduct, String>{

private static final CategoryProductDao instance = new CategoryProductDao();
	
	private CategoryProductDao(){}
	
	public synchronized static CategoryProductDao getInstance(){
		return instance;
	}
	public boolean saveCategoryIdandProductId(Connection conn,String categoryId ,String productId) {
        boolean b = false;
        PreparedStatement stmt = null;
        PreparedStatement synStmt = null;
        try
        {
            String sql = " insert into p_category_product(category_id,product_id) values(?,?) ";
            stmt = conn.prepareStatement(sql);
                              stmt.setString(1,categoryId);
                              stmt.setString(2,productId);
            stmt.execute();
            //增加同步数据
            String synSql = " insert into p_category_product_syn ( category_id,product_id,d_sysctime,c_sysc_status,c_issysc) values(?,?,NOW(),?,?)";
            synStmt = conn.prepareStatement(synSql);
            synStmt.setString(1,categoryId);
            synStmt.setString(2,productId);
            synStmt.setString(3, CUDEnum.C.toString());
            synStmt.setString(4, "1");
            synStmt.execute();
            b = true ;
        }catch(Exception e){
        	e.printStackTrace();
        }
        finally{
        	this.close(stmt);
        	this.close(synStmt);
        }
        return b;
    }
	public List<String> findCategoryIdbyProductId(Connection conn,String productId) throws SQLException{
        List<String> list  = new ArrayList<String>();
        PreparedStatement stmt=null ;
        try
        {
        String sql = "select category_id from p_category_product where product_id = ?";
        stmt = conn.prepareStatement(sql);
                          stmt.setString(1,productId);
        ResultSet rs  = stmt.executeQuery();  
        //目前只做 一个商品属于一个分类
        while(rs.next()){ 
            list.add(rs.getString(1));
           }                  
        }
        finally{
        	this.close(stmt);
        }
        return list;
    }
	 /**
     * <一句话功能简述>
     * <功能详细描述>
     * @param productId
     * @return
	 * @throws SQLException 
     * @see [类、类#方法、类#成员]
     */
    public List<Category> findCategoryListbyProductId(Connection conn,String productId) throws SQLException{
        List<Category> list  = new ArrayList<Category>();

        Category c = null;
        PreparedStatement stmt = null;
        try
        {
        String sql = "select a.c_id,a.c_name,a.c_pid,a.c_desc from p_category a,p_category_product b where a.c_id = b.category_id and b.product_id = ? ";
        stmt = conn.prepareStatement(sql);
                          stmt.setString(1,productId);
        ResultSet rs  = stmt.executeQuery();  
        while(rs.next()){ 
            c = new Category();
            c.setId(rs.getString(1));
            c.setName(rs.getString(2));
            c.setPid(rs.getString(3));
            c.setDesc(rs.getString(4));
            list.add(c);
           }                  
        }finally{
            this.close(stmt);
        }
        return list;
    }
    
    public boolean deleteAllCategorybyProductId(Connection conn,String productId){
        boolean b = false;
        PreparedStatement stmt = null;
        PreparedStatement synStmt = null;
        try
        {
        	String synSql = "insert into p_category_product_syn(category_id,product_id,d_sysctime,c_sysc_status,c_issysc) select category_id,product_id ,NOW() ,'D','1' from p_category_product where product_id = ?";
        	synStmt = conn.prepareStatement(synSql);
        	synStmt.setString(1,productId);
        	synStmt.execute();
            String sql = " delete from p_category_product where product_id = ? ";
            stmt = conn.prepareStatement(sql);
                              stmt.setString(1,productId);
            stmt.executeUpdate();
            
            b = true ;
        }catch(Exception e){
        	e.printStackTrace();
        }finally{
        	this.close(stmt);
        	this.close(synStmt);
        }
        return b;
    }
}
